package com.lcy.springtest.conf;

import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.context.annotation.Condition;
import org.springframework.context.annotation.ConditionContext;
import org.springframework.core.env.Environment;
import org.springframework.core.type.AnnotatedTypeMetadata;

/**
 * @author lichenyang
 * @create 2021-04-16 3:03 下午
 */
public class WindowsConditional implements Condition {
    /**
     * @param context: 判断条件能试用的上下文(环境)
     * @param metadata: 注释信息
     * @return
     */
    @Override
    public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
        // context 可以获取 ioc 使用的 BeanFactory 信息
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        //context 可以获取类加载器
        ClassLoader classLoader = context.getClassLoader();
        // context 获取bean 定义的注册类
        BeanDefinitionRegistry registry = context.getRegistry();

        //context 可以获取当前环境信息
        Environment environment = context.getEnvironment();
        String property = environment.getProperty("os.name");
        if(property.contains("Windows")){
            return true;
        }
        return false;
    }
}
